//
//  PhotoViewController.m
//  3DModelCollector
//
//  Created by Jing Zhang on 7/16/12.
//  Copyright (c) 2012 Tencent. All rights reserved.
//

#import "PhotoViewController.h"

@interface PhotoViewController ()

@property (retain, nonatomic) IBOutlet UIScrollView *photoContainer;

- (void)onClickCancelBarButton:(id)sender;
- (void)onClickDelBarButton:(id)sender;
- (void)showPhotos;

@end

@implementation PhotoViewController

@synthesize photoList = _photoList;
@synthesize delegate = _delegate;

@synthesize photoContainer = _photoContainer;
@synthesize curIndex = _curIndex;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIBarButtonItem* cancelBarButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(onClickCancelBarButton:)];
        self.navigationItem.leftBarButtonItem = cancelBarButton;
        [cancelBarButton release];
        
        UIBarButtonItem* delBarButton = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleBordered target:self action:@selector(onClickDelBarButton:)];
        self.navigationItem.rightBarButtonItem = delBarButton;
        [delBarButton release];
        
        self.title = @"照片查看";
    }
    return self;
}

- (void)dealloc {
    
    [_photoList release];
    self.delegate = nil;
    [_photoContainer release];

    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (self.photoList == nil || self.photoList.count == 0) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
 
    [self showPhotos];
}

- (void)viewDidUnload
{
    [self setPhotoContainer:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - [UIActionSheetDelegate]

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == actionSheet.destructiveButtonIndex) {
        NSLog(@"Delete photo");
        
        if (self.delegate != nil) {
            [self.delegate deletePhoto:self.curIndex];
        }
    }
}

#pragma mark - [Private methods]

- (void)onClickCancelBarButton:(id)sender {
    
    [self.navigationController dismissModalViewControllerAnimated:YES];
}

- (void)onClickDelBarButton:(id)sender {
    
    UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:nil];
    [actionSheet showInView:self.view];
    [actionSheet release];
}

- (void)showPhotos {
    
}

@end
